﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace PolePosition.v3.Desktop.Selectors.Controls
{
    public partial class DistributionProperties : DialogEditBase
    {
        public DistributionProperties()
        {
            InitializeComponent();
            BindLists();
        }

        private void DistributionProperties_Load(object sender, EventArgs e)
        {
            
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedPhaseList
        {
            get { return listSelectorPhase.GetSelectedValueList<int>(); }
            set
            {
                listSelectorPhase.SetSelectedValueList<int>(value);
                if (!listSelectorPhase.AllItemsChecked)
                    checkBoxXViewAll.Checked = false;
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedTopTypeList
        {
            get { return listSelectorTopType.GetSelectedValueList<int>(); }
            set
            {
                listSelectorTopType.SetSelectedValueList<int>(value);
                if (!listSelectorTopType.AllItemsChecked)
                    checkBoxXViewAll.Checked = false;
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedClassList
        {
            get { return listSelectorClass.GetSelectedValueList<int>(); }
            set
            {
                listSelectorClass.SetSelectedValueList<int>(value);
                if (!listSelectorClass.AllItemsChecked)
                    checkBoxXViewAll.Checked = false;
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedSpeciesList
        {
            get { return listSelectorPoleSpecies.GetSelectedValueList<int>(); }
            set
            {
                listSelectorPoleSpecies.SetSelectedValueList<int>(value);
                if (!listSelectorPoleSpecies.AllItemsChecked)
                    checkBoxXViewAll.Checked = false;
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedTreatmentList
        {
            get { return listSelectorPoleTreatment.GetSelectedValueList<int>(); }
            set
            {
                listSelectorPoleTreatment.SetSelectedValueList<int>(value);
                if (!listSelectorPoleTreatment.AllItemsChecked)
                    checkBoxXViewAll.Checked = false;
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedYearList
        {
            get { return listSelectorPoleYear.GetSelectedValueList<int>(); }
            set
            {
                listSelectorPoleYear.SetSelectedValueList<int>(value);
                if (!listSelectorPoleYear.AllItemsChecked)
                    checkBoxXViewAll.Checked = false;
            }
        }

        private void BindLists()
        {
            if (Program.LookupTableData != null)
            {
                listSelectorPhase.DisplayMember = EntityLibrary.DistributionPhaseEnumerations.Columns.Name.ToString();
                listSelectorPhase.ValueMember = EntityLibrary.DistributionPhaseEnumerations.Columns.PhaseId.ToString();
                listSelectorPhase.DataSource = Program.LookupTableData.DistributionPhases;
                listSelectorPhase.AddBlankOption<EntityLibrary.DistributionPhaseEntity>();

                listSelectorTopType.DisplayMember = EntityLibrary.DistributionsTopTypeEnumerations.Columns.Name.ToString();
                listSelectorTopType.ValueMember = EntityLibrary.DistributionsTopTypeEnumerations.Columns.Distributions_TopTypeId.ToString();
                listSelectorTopType.DataSource = Program.LookupTableData.DistributionsTopTypes;
                listSelectorTopType.AddBlankOption<EntityLibrary.DistributionsTopTypeEntity>();

                listSelectorClass.DisplayMember = EntityLibrary.DistributionClassesEnumerations.Columns.Name.ToString();
                listSelectorClass.ValueMember = EntityLibrary.DistributionClassesEnumerations.Columns.ClassId.ToString();
                listSelectorClass.DataSource = Program.LookupTableData.DistributionClasses;
                listSelectorClass.AddBlankOption<EntityLibrary.DistributionClassesEntity>();

                listSelectorPoleSpecies.DisplayMember = EntityLibrary.XMissionStructuresPoleSpeciesEnumerations.Columns.Name.ToString();
                listSelectorPoleSpecies.ValueMember = EntityLibrary.XMissionStructuresPoleSpeciesEnumerations.Columns.XMissionStructures_PoleSpeciesId.ToString();
                listSelectorPoleSpecies.DataSource = Program.LookupTableData.XMissionStructuresPoleSpecies;
                listSelectorPoleSpecies.AddBlankOption<EntityLibrary.XMissionStructuresPoleSpeciesEntity>();

                listSelectorPoleTreatment.DisplayMember = EntityLibrary.XMissionStructuresPoleTreatmentEnumerations.Columns.Name.ToString();
                listSelectorPoleTreatment.ValueMember = EntityLibrary.XMissionStructuresPoleTreatmentEnumerations.Columns.XMissionStructures_PoleTreatmentId.ToString();
                listSelectorPoleTreatment.DataSource = Program.LookupTableData.XMissionStructuresPoleTreatments;
                listSelectorPoleTreatment.AddBlankOption<EntityLibrary.XMissionStructuresPoleTreatmentEntity>();

                listSelectorPoleYear.DisplayMember = "Text";
                listSelectorPoleYear.ValueMember = "Value";
                listSelectorPoleYear.DataSource = ComboOptions.PoleYears;
                listSelectorPoleYear.AddBlankOption<Common.BindingLists.ListItem>();

                SetListsEnabled();
            }
        }

        private void checkBoxXViewAll_CheckedChanged(object sender, EventArgs e)
        {
            SetListsEnabled();
        }

        private void SetListsEnabled()
        {
            bool _Enabled = !checkBoxXViewAll.Checked;

            foreach (Control _List in panelSelectors.Controls)
                if (_List is ListSelector)
                {
                    (_List as ListSelector).Enabled = _Enabled;

                    foreach(ListViewItem item in  (_List as ListSelector).ListItems)
                        item.Checked = !_Enabled;
                }
        }

        public void Reset()
        {
            checkBoxXViewAll.Checked = true;
            
            if (comboBoxExPlacemarkText.Items.Count > 0)
                comboBoxExPlacemarkText.SelectedIndex = 0;
            
            SetListsEnabled();
        }
    }
}
